Meteor.publish("userrequests", function () {
  if (this.userId) {
    if (Roles.userIsInRole(this.userId, 'driver')) {
      return UserRequests.find({
        'state': 'new'
      },{
        limit: 10
      });
    } else {
      return UserRequests.find({ 'state': 'new', 'userId': this.userId });
    }
  } else {
    return;
  }

});


Meteor.publish("myrequest", function () {
  if (this.userId) {
    if (!Roles.userIsInRole(this.userId, 'driver')) {
      return UserRequests.find({ 'state': 'new', 'userid': this.userId});
    }
  }
});


Meteor.methods({
  addRequest: function() {
    if (!this.userId) {
      throw new Meteor.Error("未登录！",
        "必须先登录！");
      return;
    };

    if (UserRequests.findOne({'userId': this.userId, 'state': 'new'})) {
      throw new Meteor.Error("已经发送",
        "不能重新发送");
      return;
    }


    var user = Meteor.user();
    var req = {
      userId: user._id,
      time: new Date(),
      location: user.location,
      address: user.formatted_address,
      tel: user.others.tel,
      state: 'new',/**/
    };

    UserRequests.insert(req);
  },

  cancelRequest: function (userId) {
    if (!this.userId) {
      throw new Meteor.Error("未登录！",
        "必须先登录！");
      return;
    };

    var req = UserRequests.findOne({ 'state': 'new', 'userId': userId });
    if (!req)
      return;

    UserRequests.update({
      'userId': userId,
      'state' : 'new'
    }, {
      $set: {
        'state': 'cancel'
      }
    });

  },

  getRequest: function (reqId) {
    if (!this.userId)
      return;
    if (!Roles.userIsInRole(this.userId, 'driver'))
      return;

    var req = UserRequests.findOne({ 'state': 'new', '_id': reqId });
    if (!req)
      return;

    var driverInfo = Meteor.users.findOne({ '_id': this.userId});
    var userInfo = Meteor.users.findOne({ '_id': req.userId});

    Orders.insert({
      'driverId': this.userId,
      'reqId': reqId,
      'state': 'accept',
      'userId': req.userId,
      'acceptTime': new Date(),
      'driverInfo': driverInfo.others,
      'userInfo': userInfo.others,
    });

    UserRequests.upsert(reqId, {
      $set: {
        'driverId': this.userId,
        'state': 'accept'
      }
    });

    return {
      state: true
    }
  },

  setDestination: function (name, district, lng, lat) {
    if (!this.userId)
      return;

    var req = UserRequests.findOne({ 'state': 'new', 'userId': this.userId });
    if (!req)
      return;

    UserRequests.upsert({
        'state': 'new',
        'userId': this.userId
      }, {
        $set: {
          'desName': name,
          'desDistrict': district,
          'desLocation': {'lat': lat, 'lng': lng}
      }
    }, function (err) {
      console.log(err);
    });

    return name;
  },

  setDistance: function (distance) {
    if (!this.userId)
      return;

    var req = UserRequests.findOne({ 'state': 'new', 'userId': this.userId });
    if (!req)
      return;

    UserRequests.upsert({
        'state': 'new',
        'userId': this.userId
      }, {
        $set: {
          'distance': distance,
      }
    }, function (err) {
      console.log(err);
    });

  },

  getRequestInfo: function (reqId) {
    if (!this.userId)
      return;

    var req = UserRequests.findOne({ '_id': reqId });

    return req;
  },
});
